package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.AnAnimal;
import com.animal.entity.AnApply;
import com.animal.entity.AnCollect;
import com.animal.entity.AnMeans;
import com.animal.mapper.AnApplyMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.response.Result;
import com.animal.service.AnApplyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/an-apply")
public class AnApplyController extends JSONAuthentication {
    @Autowired
    AnApplyMapper anApplyMapper;

    @Autowired
    AnApplyService anApplyService;
    @Autowired
    AnUserMapper anUserMapper;

    @ApiOperation(value = "添加申请")
    @PostMapping("/add")
    public void addApply(HttpServletRequest request, HttpServletResponse response, @RequestBody AnApply anApply) throws  Exception{

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",anApply.getUserId());
        if(anApplyMapper.selectList(queryWrapper).size()>0){
            List<AnApply> list=anApplyMapper.selectList(queryWrapper);
            for(AnApply a:list){
                if(a.getUserId().equals(anApply.getUserId())&&a.getAnId().equals(anApply.getAnId())){
                    Result error = Result.error().message("不可重复申请");
                    this.WriteJSON(request, response, error);
                    return;
                }

            }
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            anApply.setSqTime(timestamp);
            int n= anApplyMapper.insert(anApply);
            if(n>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
            return;
        }else{
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            anApply.setSqTime(timestamp);
            int n= anApplyMapper.insert(anApply);
            if(n>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }


    }

    @ApiOperation(value = "删除申请")
    @PostMapping("/del")
    public void delApply(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=anApplyMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改申请")
    @PostMapping("/upd")
    public void updMean(HttpServletRequest request, HttpServletResponse response, @RequestBody AnApply anApply)throws Exception{

        int i=anApplyMapper.updateById(anApply);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }


    }

    @ApiOperation(value = "查询申请列表")
    @PostMapping("/look")
    public void lookAnimal(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{
        int total=anApplyMapper.selectCount(null);
        List<AnApply> list =anApplyService.findAll(current,size);
        Result ok = Result.ok().data("anapply",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "通过申请id修改申请")
    @PostMapping("/updById")
    public void updMeana(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("sqStatus") int sqStatus)throws Exception{
        AnApply anApply=anApplyMapper.selectById(id);
        anApply.setSqStatus(sqStatus);
        int i=anApplyMapper.updateById(anApply);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过用户id查看申请")
    @PostMapping("/lookById")
    public void updMeanabb(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId,@RequestParam("current") int current,@RequestParam("size") int size)throws Exception{

        List<AnApply> i=anApplyService.findByUserId(current,size,userId);
        if(i.size()>0){
            Result ok = Result.ok().data("apply",i);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

